package uk.co.alt236.bluetoothlelib.device.adrecord;

import ohos.utils.Parcel;
import ohos.utils.Sequenceable;
import uk.co.alt236.bluetoothlelib.util.AdRecordUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;

/**
 * The Class AdRecordStore.
 */
public class AdRecordStore implements Sequenceable {

    private HashMap<Integer, AdRecord> mAdRecords;
    private String mLocalNameComplete;
    private String mLocalNameShort;

    public AdRecordStore(){
        
    }
    
    /**
     * Instantiates a new Bluetooth LE device Ad Record Store.
     *
     * @param adRecords the ad records
     */
    public AdRecordStore(final HashMap<Integer, AdRecord> adRecords) {
        mAdRecords = adRecords;
        mLocalNameComplete = AdRecordUtils.getRecordDataAsString(mAdRecords.get(AdRecord.TYPE_LOCAL_NAME_COMPLETE));
        mLocalNameShort = AdRecordUtils.getRecordDataAsString(
                mAdRecords.get(AdRecord.TYPE_LOCAL_NAME_SHORT));

    }

    //反序列化功能
    public static final Producer<AdRecordStore> PRODUCER = new Producer<AdRecordStore>() {

        public AdRecordStore createFromParcel(Parcel in) {
            // Initialize an instance first, then do customized unmarshlling.
            AdRecordStore adrecordstore = new AdRecordStore();
            adrecordstore.unmarshalling(in);
            return adrecordstore;
        }

        public AdRecordStore[] newArray(final int size) {
            return new AdRecordStore[size];
        }
    };

    /**
     * Gets the short local device name.
     *
     * @return the local name complete
     */
    public String getLocalNameComplete() {
        return mLocalNameComplete;
    }

    /**
     * Gets the complete local device name.
     *
     * @return the local name short
     */
    public String getLocalNameShort() {
        return mLocalNameShort;
    }

    /**
     * retrieves an individual record.
     *
     * @param record the record
     * @return the record
     */
    public AdRecord getRecord(final int record) {
        return mAdRecords.get(record);
    }

    /**
     * Gets the record data as string.
     *
     * @param record the record
     * @return the record data as string
     */
    public String getRecordDataAsString(final int record) {
        return AdRecordUtils.getRecordDataAsString(
                mAdRecords.get(record));
    }

    /**
     * Gets the record as collection.
     *
     * @return the records as collection
     */
    public Collection<AdRecord> getRecordsAsCollection() {
        return Collections.unmodifiableCollection(asList(mAdRecords));
    }

    /**
     * Checks if is record present.
     *
     * @param record the record
     * @return true, if is record present
     */
    public boolean isRecordPresent(final int record) {
        return mAdRecords.containsKey(record);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "AdRecordStore [mLocalNameComplete=" + mLocalNameComplete + ", mLocalNameShort=" + mLocalNameShort + "]";
    }

    /**
     * As list.
     *
     * @param <C>         the generic type
     * @param sparseArray the sparse array
     * @return the collection
     */
    public static <C> Collection<C> asList(final HashMap<Integer, C> sparseArray) {
        if (sparseArray == null){
            return null;
        }

        final Collection<C> arrayList = new ArrayList<>(sparseArray.size());
        for (int i = 0; i < sparseArray.size(); i++) {
            arrayList.add(sparseArray.get(i));
        }
        return arrayList;
    }

    @Override
    public boolean marshalling(Parcel parcel) {
        parcel.writeMap(mAdRecords);
        parcel.writeString(mLocalNameComplete);
        parcel.writeString(mLocalNameShort);
        return true;
    }

    @Override
    public boolean unmarshalling(Parcel parcel) {
        mAdRecords = (HashMap<Integer, AdRecord>) parcel.readMap();
        mLocalNameComplete = parcel.readString();
        mLocalNameShort = parcel.readString();
        return true;
    }
}
